import { ZM_SERVICE_ID } from '@/config'

const zmPlugin = requirePlugin('zmPlugin')

export default {
	data () {
		return {
			creditInfo: {}
		}
	},
	methods: {
		/*
		 * 信用预付信息
		*/
		async checkCredit (showCreditPopup = false) {
			const [res] = await this.$apis.checkCredit({
				evaluateid: this.evaluateInfo.evaluateid || this.evaluateId
			})
			if (res._errCode === '0') {
				const creditInfo = res._data || {}
				creditInfo.isPrepay = false
				this.creditInfo = creditInfo
				if (showCreditPopup) {
					this.openCreditResult()
				}
			}
		},
		/*
		 * 开通信用预付
		*/
		async openCredit () {
			uni.showLoading({
				title: '',
				mask: true
			})
			const [res] = await this.$apis.openCredit()
			uni.hideLoading()
			if (res._errCode === '0') {
				const openInfo = res._data || {}
				if (!openInfo.signStr) {
					return uni.showToast({
						title: '开通失败（signStr不能为空）',
						icon: 'none'
					})
				}
				this.creditInfo = {
					...this.creditInfo,
					outAgreementNo: openInfo.outAgreementNo || '',
					creditAction: true
				}
				zmPlugin.startService({
					type: 'pay_after',
					sign_str: openInfo.signStr,
					zm_service_id: ZM_SERVICE_ID
				})
			} else {
				uni.showToast({
					title: res._errStr || '获取开通信息失败',
					icon: 'none'
				})
			}
		},
		/*
		 * 信用预付信息
		*/
		async checkCreditStatusByOutNo () {
			if (!this.creditInfo.outAgreementNo) {
				return this.checkCredit(true)
			}
			const [res] = await this.$apis.checkCreditStatusByOutNo({
				outAgreementNo: this.creditInfo.outAgreementNo
			})
			if (res._errCode === '0') {
				const status = res._data.status
				if (status) {
					this.creditInfo.status = status
					if (status === '3') {
						if (!this.creditInfo.isPrepay) {
							this.changePrepay()
						}
					}
				}
				this.openCreditResult()
			}
		},
		changePrepay () {
			this.creditInfo.isPrepay = !this.creditInfo.isPrepay
		},
		openCreditResult () {
			this.$refs.recycleWayFormCom.openCreditResultPopup()
		}
	}
}